/**
 * ClassName: OrderController
 * Package: org.example.controller
 * Description: 订单控制器-调用基于XML的OrderService
 *
 * @Author BCXJ
 * @Create 2025/10/6 02:15
 * @Version 1.0
 * @Since 1.0
 */
package org.example.controller;

import org.example.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 订单控制器
 * 通过XML配置引用远程OrderService服务
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);
    
    /**
     * 通过Spring注入OrderService
     * 实际是Dubbo的远程服务代理
     */
    @Resource
    private OrderService orderService;
    
    /**
     * 获取订单信息接口
     * 
     * @param orderId 订单ID
     * @return 订单信息
     */
    @RequestMapping("/getOrderInfo")
    @ResponseBody
    public String getOrderInfo(@RequestParam("orderId") Long orderId) {
        logger.info("【Web消费者】收到请求: /order/getOrderInfo, orderId={}", orderId);
        
        try {
            String orderInfo = orderService.getOrderInfo(orderId);
            logger.info("【Web消费者】调用OrderService成功，返回: {}", orderInfo);
            return "成功！" + orderInfo;
        } catch (Exception e) {
            logger.error("【Web消费者】调用OrderService失败", e);
            return "失败！错误信息: " + e.getMessage();
        }
    }
    
    /**
     * 创建订单接口
     * 
     * @param userId 用户ID
     * @param productId 产品ID
     * @return 订单创建结果
     */
    @RequestMapping("/createOrder")
    @ResponseBody
    public String createOrder(@RequestParam("userId") Long userId, 
                             @RequestParam("productId") Long productId) {
        logger.info("【Web消费者】收到请求: /order/createOrder, userId={}, productId={}", userId, productId);
        
        try {
            String result = orderService.createOrder(userId, productId);
            logger.info("【Web消费者】调用OrderService成功，返回: {}", result);
            return "成功！" + result;
        } catch (Exception e) {
            logger.error("【Web消费者】调用OrderService失败", e);
            return "失败！错误信息: " + e.getMessage();
        }
    }
}

